/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "downmix.h"

/* coefficient matrix for downmixing */
/* usage: [output_channel_layout][input_channel][output_channel*/
const uint16_t channelDownmixMatrix[PA_CHANNEL_LAYOUT_COUNT][PA_DOWNMIX_CHANNEL_MAX][PA_DOWNMIX_CHANNEL_MAX] = {
    /* Downmix matrix for STEREO output */
    [PA_CHANNEL_LAYOUT_STEREO][PA_CHANNEL_POSITION_FRONT_LEFT][PA_CHANNEL_POSITION_FRONT_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_STEREO][PA_CHANNEL_POSITION_FRONT_RIGHT][PA_CHANNEL_POSITION_FRONT_RIGHT] = 10000,
    [PA_CHANNEL_LAYOUT_STEREO][PA_CHANNEL_POSITION_CENTER][PA_CHANNEL_POSITION_FRONT_LEFT] = 7071,
    [PA_CHANNEL_LAYOUT_STEREO][PA_CHANNEL_POSITION_CENTER][PA_CHANNEL_POSITION_FRONT_RIGHT] = 7071,
    [PA_CHANNEL_LAYOUT_STEREO][PA_CHANNEL_POSITION_REAR_LEFT][PA_CHANNEL_POSITION_FRONT_LEFT] = 7071,
    [PA_CHANNEL_LAYOUT_STEREO][PA_CHANNEL_POSITION_REAR_RIGHT][PA_CHANNEL_POSITION_FRONT_RIGHT] = 7071,
    [PA_CHANNEL_LAYOUT_STEREO][PA_CHANNEL_POSITION_REAR_CENTER][PA_CHANNEL_POSITION_FRONT_LEFT] = 5000,
    [PA_CHANNEL_LAYOUT_STEREO][PA_CHANNEL_POSITION_REAR_CENTER][PA_CHANNEL_POSITION_FRONT_RIGHT] = 5000,
    [PA_CHANNEL_LAYOUT_STEREO][PA_CHANNEL_POSITION_SIDE_LEFT][PA_CHANNEL_POSITION_FRONT_LEFT] = 7071,
    [PA_CHANNEL_LAYOUT_STEREO][PA_CHANNEL_POSITION_SIDE_RIGHT][PA_CHANNEL_POSITION_FRONT_RIGHT] = 7071,
    [PA_CHANNEL_LAYOUT_STEREO][PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER][PA_CHANNEL_POSITION_FRONT_LEFT] = 6057,
    [PA_CHANNEL_LAYOUT_STEREO][PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER][PA_CHANNEL_POSITION_FRONT_RIGHT] = 2509,
    [PA_CHANNEL_LAYOUT_STEREO][PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER][PA_CHANNEL_POSITION_FRONT_LEFT] = 2509,
    [PA_CHANNEL_LAYOUT_STEREO][PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER][PA_CHANNEL_POSITION_FRONT_RIGHT] = 6057,
    [PA_CHANNEL_LAYOUT_STEREO][PA_DOWNMIX_POSITION_TOP_CENTER][PA_CHANNEL_POSITION_FRONT_LEFT] = 3552,
    [PA_CHANNEL_LAYOUT_STEREO][PA_DOWNMIX_POSITION_TOP_CENTER][PA_CHANNEL_POSITION_FRONT_RIGHT] = 3552,
    [PA_CHANNEL_LAYOUT_STEREO][PA_DOWNMIX_POSITION_TOP_FRONT_LEFT][PA_CHANNEL_POSITION_FRONT_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_STEREO][PA_DOWNMIX_POSITION_TOP_FRONT_RIGHT][PA_CHANNEL_POSITION_FRONT_RIGHT] = 10000,
    [PA_CHANNEL_LAYOUT_STEREO][PA_DOWNMIX_POSITION_TOP_FRONT_CENTER][PA_CHANNEL_POSITION_FRONT_LEFT] = 7071,
    [PA_CHANNEL_LAYOUT_STEREO][PA_DOWNMIX_POSITION_TOP_FRONT_CENTER][PA_CHANNEL_POSITION_FRONT_RIGHT] = 7071,
    [PA_CHANNEL_LAYOUT_STEREO][PA_DOWNMIX_POSITION_TOP_REAR_LEFT][PA_CHANNEL_POSITION_FRONT_LEFT] = 7071,
    [PA_CHANNEL_LAYOUT_STEREO][PA_DOWNMIX_POSITION_TOP_REAR_RIGHT][PA_CHANNEL_POSITION_FRONT_RIGHT] = 7071,
    [PA_CHANNEL_LAYOUT_STEREO][PA_DOWNMIX_POSITION_TOP_REAR_CENTER][PA_CHANNEL_POSITION_FRONT_LEFT] = 3544,
    [PA_CHANNEL_LAYOUT_STEREO][PA_DOWNMIX_POSITION_TOP_REAR_CENTER][PA_CHANNEL_POSITION_FRONT_RIGHT] = 3544,
    
    /* Downmix matrix for 5POINT1 */
    [PA_CHANNEL_LAYOUT_5POINT1][PA_CHANNEL_POSITION_FRONT_LEFT][PA_CHANNEL_POSITION_FRONT_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1][PA_CHANNEL_POSITION_FRONT_RIGHT][PA_CHANNEL_POSITION_FRONT_RIGHT] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1][PA_CHANNEL_POSITION_FRONT_CENTER][PA_CHANNEL_POSITION_FRONT_CENTER] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1][PA_CHANNEL_POSITION_LFE][PA_CHANNEL_POSITION_LFE] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1][PA_CHANNEL_POSITION_REAR_LEFT][PA_CHANNEL_POSITION_SIDE_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1][PA_CHANNEL_POSITION_REAR_RIGHT][PA_CHANNEL_POSITION_SIDE_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1][PA_CHANNEL_POSITION_SIDE_LEFT][PA_CHANNEL_POSITION_SIDE_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1][PA_CHANNEL_POSITION_SIDE_RIGHT][PA_CHANNEL_POSITION_SIDE_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1][PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER][PA_CHANNEL_POSITION_FRONT_LEFT] = 5946,
    [PA_CHANNEL_LAYOUT_5POINT1][PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER][PA_CHANNEL_POSITION_FRONT_CENTER] = 7071,
    [PA_CHANNEL_LAYOUT_5POINT1][PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER][PA_CHANNEL_POSITION_FRONT_RIGHT] = 5946,
    [PA_CHANNEL_LAYOUT_5POINT1][PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER][PA_CHANNEL_POSITION_FRONT_CENTER] = 7071,
    [PA_CHANNEL_LAYOUT_5POINT1][PA_CHANNEL_POSITION_REAR_CENTER][PA_CHANNEL_POSITION_SIDE_LEFT] = 7071,
    [PA_CHANNEL_LAYOUT_5POINT1][PA_CHANNEL_POSITION_REAR_CENTER][PA_CHANNEL_POSITION_SIDE_RIGHT] = 7071,
    [PA_CHANNEL_LAYOUT_5POINT1][PA_DOWNMIX_POSITION_TOP_CENTER][PA_CHANNEL_POSITION_SIDE_LEFT] = 5000,
    [PA_CHANNEL_LAYOUT_5POINT1][PA_DOWNMIX_POSITION_TOP_CENTER][PA_CHANNEL_POSITION_SIDE_RIGHT] = 5000,
    [PA_CHANNEL_LAYOUT_5POINT1][PA_DOWNMIX_POSITION_TOP_CENTER][PA_CHANNEL_POSITION_FRONT_CENTER] = 5000,
    [PA_CHANNEL_LAYOUT_5POINT1][PA_DOWNMIX_POSITION_TOP_FRONT_LEFT][PA_CHANNEL_POSITION_FRONT_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1][PA_DOWNMIX_POSITION_TOP_FRONT_RIGHT][PA_CHANNEL_POSITION_FRONT_RIGHT] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1][PA_DOWNMIX_POSITION_TOP_FRONT_CENTER][PA_CHANNEL_POSITION_FRONT_CENTER] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1][PA_DOWNMIX_POSITION_TOP_REAR_LEFT][PA_CHANNEL_POSITION_SIDE_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1][PA_DOWNMIX_POSITION_TOP_REAR_RIGHT][PA_CHANNEL_POSITION_SIDE_RIGHT] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1][PA_DOWNMIX_POSITION_TOP_REAR_CENTER][PA_CHANNEL_POSITION_SIDE_LEFT] = 7071,
    [PA_CHANNEL_LAYOUT_5POINT1][PA_DOWNMIX_POSITION_TOP_REAR_CENTER][PA_CHANNEL_POSITION_SIDE_RIGHT] = 7071,

    /* Downmix table for 5POINT1POINT2 */
    [PA_CHANNEL_LAYOUT_5POINT1POINT2][PA_CHANNEL_POSITION_FRONT_LEFT][PA_CHANNEL_POSITION_FRONT_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1POINT2][PA_CHANNEL_POSITION_FRONT_RIGHT][PA_CHANNEL_POSITION_FRONT_RIGHT] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1POINT2][PA_CHANNEL_POSITION_FRONT_CENTER][PA_CHANNEL_POSITION_FRONT_CENTER] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1POINT2][PA_CHANNEL_POSITION_LFE][PA_CHANNEL_POSITION_LFE] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1POINT2][PA_CHANNEL_POSITION_REAR_LEFT][PA_CHANNEL_POSITION_SIDE_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1POINT2][PA_CHANNEL_POSITION_REAR_RIGHT][PA_CHANNEL_POSITION_SIDE_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1POINT2][PA_CHANNEL_POSITION_SIDE_LEFT][PA_CHANNEL_POSITION_SIDE_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1POINT2][PA_CHANNEL_POSITION_SIDE_RIGHT][PA_CHANNEL_POSITION_SIDE_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1POINT2][PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER][PA_CHANNEL_POSITION_FRONT_LEFT] = 5946,
    [PA_CHANNEL_LAYOUT_5POINT1POINT2][PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER][PA_CHANNEL_POSITION_FRONT_CENTER] =
        7071,
    [PA_CHANNEL_LAYOUT_5POINT1POINT2][PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER][PA_CHANNEL_POSITION_FRONT_RIGHT] =
        5946,
    [PA_CHANNEL_LAYOUT_5POINT1POINT2][PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER][PA_CHANNEL_POSITION_FRONT_CENTER] =
        7071,
    [PA_CHANNEL_LAYOUT_5POINT1POINT2][PA_CHANNEL_POSITION_REAR_CENTER][PA_CHANNEL_POSITION_SIDE_LEFT] = 7071,
    [PA_CHANNEL_LAYOUT_5POINT1POINT2][PA_CHANNEL_POSITION_REAR_CENTER][PA_CHANNEL_POSITION_SIDE_RIGHT] = 7071,
    [PA_CHANNEL_LAYOUT_5POINT1POINT2][PA_DOWNMIX_POSITION_TOP_CENTER][PA_DOWNMIX_POSITION_TOP_REAR_LEFT] = 7071,
    [PA_CHANNEL_LAYOUT_5POINT1POINT2][PA_DOWNMIX_POSITION_TOP_CENTER][PA_DOWNMIX_POSITION_TOP_REAR_RIGHT] = 7071,
    [PA_CHANNEL_LAYOUT_5POINT1POINT2][PA_DOWNMIX_POSITION_TOP_FRONT_LEFT][PA_CHANNEL_POSITION_FRONT_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1POINT2][PA_DOWNMIX_POSITION_TOP_FRONT_RIGHT][PA_CHANNEL_POSITION_FRONT_RIGHT] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1POINT2][PA_DOWNMIX_POSITION_TOP_FRONT_CENTER][PA_CHANNEL_POSITION_FRONT_CENTER] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1POINT2][PA_DOWNMIX_POSITION_TOP_REAR_LEFT][PA_DOWNMIX_POSITION_TOP_REAR_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1POINT2][PA_DOWNMIX_POSITION_TOP_REAR_RIGHT][PA_DOWNMIX_POSITION_TOP_REAR_RIGHT] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1POINT2][PA_DOWNMIX_POSITION_TOP_REAR_CENTER][PA_CHANNEL_POSITION_REAR_LEFT] = 7071,
    [PA_CHANNEL_LAYOUT_5POINT1POINT2][PA_DOWNMIX_POSITION_TOP_REAR_CENTER][PA_CHANNEL_POSITION_REAR_RIGHT] = 7071,

    /* Downmix table for 5POINT1POINT4 */
    [PA_CHANNEL_LAYOUT_5POINT1POINT4][PA_CHANNEL_POSITION_FRONT_LEFT][PA_CHANNEL_POSITION_FRONT_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1POINT4][PA_CHANNEL_POSITION_FRONT_RIGHT][PA_CHANNEL_POSITION_FRONT_RIGHT] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1POINT4][PA_CHANNEL_POSITION_FRONT_CENTER][PA_CHANNEL_POSITION_FRONT_CENTER] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1POINT4][PA_CHANNEL_POSITION_LFE][PA_CHANNEL_POSITION_LFE] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1POINT4][PA_CHANNEL_POSITION_REAR_LEFT][PA_CHANNEL_POSITION_SIDE_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1POINT4][PA_CHANNEL_POSITION_REAR_RIGHT][PA_CHANNEL_POSITION_SIDE_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1POINT4][PA_CHANNEL_POSITION_SIDE_LEFT][PA_CHANNEL_POSITION_SIDE_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1POINT4][PA_CHANNEL_POSITION_SIDE_RIGHT][PA_CHANNEL_POSITION_SIDE_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1POINT4][PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER][PA_CHANNEL_POSITION_FRONT_LEFT] = 5946,
    [PA_CHANNEL_LAYOUT_5POINT1POINT4][PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER][PA_CHANNEL_POSITION_FRONT_CENTER] =
        7071,
    [PA_CHANNEL_LAYOUT_5POINT1POINT4][PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER][PA_CHANNEL_POSITION_FRONT_RIGHT] =
        5946,
    [PA_CHANNEL_LAYOUT_5POINT1POINT4][PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER][PA_CHANNEL_POSITION_FRONT_CENTER] =
        7071,
    [PA_CHANNEL_LAYOUT_5POINT1POINT4][PA_CHANNEL_POSITION_REAR_CENTER][PA_CHANNEL_POSITION_SIDE_LEFT] = 7071,
    [PA_CHANNEL_LAYOUT_5POINT1POINT4][PA_CHANNEL_POSITION_REAR_CENTER][PA_CHANNEL_POSITION_SIDE_RIGHT] = 7071,
    [PA_CHANNEL_LAYOUT_5POINT1POINT4][PA_DOWNMIX_POSITION_TOP_CENTER][PA_DOWNMIX_POSITION_TOP_FRONT_LEFT] = 5000,
    [PA_CHANNEL_LAYOUT_5POINT1POINT4][PA_DOWNMIX_POSITION_TOP_CENTER][PA_DOWNMIX_POSITION_TOP_FRONT_RIGHT] = 5000,
    [PA_CHANNEL_LAYOUT_5POINT1POINT4][PA_DOWNMIX_POSITION_TOP_CENTER][PA_DOWNMIX_POSITION_TOP_REAR_LEFT] = 5000,
    [PA_CHANNEL_LAYOUT_5POINT1POINT4][PA_DOWNMIX_POSITION_TOP_CENTER][PA_DOWNMIX_POSITION_TOP_REAR_RIGHT] = 5000,
    [PA_CHANNEL_LAYOUT_5POINT1POINT4][PA_DOWNMIX_POSITION_TOP_FRONT_LEFT][PA_DOWNMIX_POSITION_TOP_FRONT_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1POINT4][PA_DOWNMIX_POSITION_TOP_FRONT_RIGHT][PA_DOWNMIX_POSITION_TOP_FRONT_RIGHT] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1POINT4][PA_DOWNMIX_POSITION_TOP_FRONT_CENTER][PA_DOWNMIX_POSITION_TOP_FRONT_LEFT] = 7071,
    [PA_CHANNEL_LAYOUT_5POINT1POINT4][PA_DOWNMIX_POSITION_TOP_FRONT_CENTER][PA_DOWNMIX_POSITION_TOP_FRONT_RIGHT] = 7071,
    [PA_CHANNEL_LAYOUT_5POINT1POINT4][PA_DOWNMIX_POSITION_TOP_REAR_LEFT][PA_DOWNMIX_POSITION_TOP_REAR_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1POINT4][PA_DOWNMIX_POSITION_TOP_REAR_RIGHT][PA_DOWNMIX_POSITION_TOP_REAR_RIGHT] = 10000,
    [PA_CHANNEL_LAYOUT_5POINT1POINT4][PA_DOWNMIX_POSITION_TOP_REAR_CENTER][PA_DOWNMIX_POSITION_TOP_REAR_LEFT] = 7071,
    [PA_CHANNEL_LAYOUT_5POINT1POINT4][PA_DOWNMIX_POSITION_TOP_REAR_CENTER][PA_DOWNMIX_POSITION_TOP_REAR_RIGHT] = 7071,

    /* Downmix table for 7POINT1 */
    [PA_CHANNEL_LAYOUT_7POINT1][PA_CHANNEL_POSITION_FRONT_LEFT][PA_CHANNEL_POSITION_FRONT_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1][PA_CHANNEL_POSITION_FRONT_RIGHT][PA_CHANNEL_POSITION_FRONT_RIGHT] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1][PA_CHANNEL_POSITION_FRONT_CENTER][PA_CHANNEL_POSITION_FRONT_CENTER] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1][PA_CHANNEL_POSITION_LFE][PA_CHANNEL_POSITION_LFE] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1][PA_CHANNEL_POSITION_REAR_LEFT][PA_CHANNEL_POSITION_REAR_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1][PA_CHANNEL_POSITION_REAR_RIGHT][PA_CHANNEL_POSITION_REAR_RIGHT] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1][PA_CHANNEL_POSITION_SIDE_RIGHT][PA_CHANNEL_POSITION_SIDE_RIGHT] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1][PA_CHANNEL_POSITION_SIDE_LEFT][PA_CHANNEL_POSITION_SIDE_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1][PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER][PA_CHANNEL_POSITION_FRONT_LEFT] = 5946,
    [PA_CHANNEL_LAYOUT_7POINT1][PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER][PA_CHANNEL_POSITION_FRONT_CENTER] = 7071,
    [PA_CHANNEL_LAYOUT_7POINT1][PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER][PA_CHANNEL_POSITION_FRONT_RIGHT] = 5946,
    [PA_CHANNEL_LAYOUT_7POINT1][PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER][PA_CHANNEL_POSITION_FRONT_CENTER] = 7071,
    [PA_CHANNEL_LAYOUT_7POINT1][PA_CHANNEL_POSITION_REAR_CENTER][PA_CHANNEL_POSITION_REAR_LEFT] = 7071,
    [PA_CHANNEL_LAYOUT_7POINT1][PA_CHANNEL_POSITION_REAR_CENTER][PA_CHANNEL_POSITION_REAR_RIGHT] = 7071,
    [PA_CHANNEL_LAYOUT_7POINT1][PA_DOWNMIX_POSITION_TOP_CENTER][PA_CHANNEL_POSITION_FRONT_CENTER] = 5000,
    [PA_CHANNEL_LAYOUT_7POINT1][PA_DOWNMIX_POSITION_TOP_CENTER][PA_CHANNEL_POSITION_SIDE_LEFT] = 5000,
    [PA_CHANNEL_LAYOUT_7POINT1][PA_DOWNMIX_POSITION_TOP_CENTER][PA_CHANNEL_POSITION_SIDE_RIGHT] = 5000,
    [PA_CHANNEL_LAYOUT_7POINT1][PA_DOWNMIX_POSITION_TOP_FRONT_LEFT][PA_CHANNEL_POSITION_FRONT_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1][PA_DOWNMIX_POSITION_TOP_FRONT_RIGHT][PA_CHANNEL_POSITION_FRONT_RIGHT] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1][PA_DOWNMIX_POSITION_TOP_FRONT_CENTER][PA_CHANNEL_POSITION_FRONT_CENTER] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1][PA_DOWNMIX_POSITION_TOP_REAR_LEFT][PA_CHANNEL_POSITION_REAR_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1][PA_DOWNMIX_POSITION_TOP_REAR_RIGHT][PA_CHANNEL_POSITION_REAR_RIGHT] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1][PA_DOWNMIX_POSITION_TOP_REAR_CENTER][PA_CHANNEL_POSITION_REAR_LEFT] = 7071,
    [PA_CHANNEL_LAYOUT_7POINT1][PA_DOWNMIX_POSITION_TOP_REAR_CENTER][PA_CHANNEL_POSITION_REAR_RIGHT] = 7071,

    /* downmix table for 7POINT1POINT2 */
    [PA_CHANNEL_LAYOUT_7POINT1POINT2][PA_CHANNEL_POSITION_FRONT_LEFT][PA_CHANNEL_POSITION_FRONT_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1POINT2][PA_CHANNEL_POSITION_FRONT_RIGHT][PA_CHANNEL_POSITION_FRONT_RIGHT] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1POINT2][PA_CHANNEL_POSITION_FRONT_CENTER][PA_CHANNEL_POSITION_FRONT_CENTER] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1POINT2][PA_CHANNEL_POSITION_LFE][PA_CHANNEL_POSITION_LFE] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1POINT2][PA_CHANNEL_POSITION_REAR_LEFT][PA_CHANNEL_POSITION_REAR_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1POINT2][PA_CHANNEL_POSITION_REAR_RIGHT][PA_CHANNEL_POSITION_REAR_RIGHT] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1POINT2][PA_CHANNEL_POSITION_SIDE_RIGHT][PA_CHANNEL_POSITION_SIDE_RIGHT] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1POINT2][PA_CHANNEL_POSITION_SIDE_LEFT][PA_CHANNEL_POSITION_SIDE_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1POINT2][PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER][PA_CHANNEL_POSITION_FRONT_LEFT] = 5946,
    [PA_CHANNEL_LAYOUT_7POINT1POINT2][PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER][PA_CHANNEL_POSITION_FRONT_CENTER] =
        7071,
    [PA_CHANNEL_LAYOUT_7POINT1POINT2][PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER][PA_CHANNEL_POSITION_FRONT_RIGHT] =
        5946,
    [PA_CHANNEL_LAYOUT_7POINT1POINT2][PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER][PA_CHANNEL_POSITION_FRONT_CENTER] =
        7071,
    [PA_CHANNEL_LAYOUT_7POINT1POINT2][PA_CHANNEL_POSITION_REAR_CENTER][PA_CHANNEL_POSITION_REAR_LEFT] = 7071,
    [PA_CHANNEL_LAYOUT_7POINT1POINT2][PA_CHANNEL_POSITION_REAR_CENTER][PA_CHANNEL_POSITION_REAR_RIGHT] = 7071,
    [PA_CHANNEL_LAYOUT_7POINT1POINT2][PA_DOWNMIX_POSITION_TOP_CENTER][PA_DOWNMIX_POSITION_TOP_REAR_LEFT] = 7071,
    [PA_CHANNEL_LAYOUT_7POINT1POINT2][PA_DOWNMIX_POSITION_TOP_CENTER][PA_DOWNMIX_POSITION_TOP_REAR_RIGHT] = 7071,
    [PA_CHANNEL_LAYOUT_7POINT1POINT2][PA_DOWNMIX_POSITION_TOP_FRONT_LEFT][PA_CHANNEL_POSITION_FRONT_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1POINT2][PA_DOWNMIX_POSITION_TOP_FRONT_RIGHT][PA_CHANNEL_POSITION_FRONT_RIGHT] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1POINT2][PA_DOWNMIX_POSITION_TOP_FRONT_CENTER][PA_CHANNEL_POSITION_FRONT_CENTER] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1POINT2][PA_DOWNMIX_POSITION_TOP_REAR_LEFT][PA_DOWNMIX_POSITION_TOP_REAR_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1POINT2][PA_DOWNMIX_POSITION_TOP_REAR_RIGHT][PA_DOWNMIX_POSITION_TOP_REAR_RIGHT] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1POINT2][PA_DOWNMIX_POSITION_TOP_REAR_CENTER][PA_DOWNMIX_POSITION_TOP_REAR_LEFT] = 7071,
    [PA_CHANNEL_LAYOUT_7POINT1POINT2][PA_DOWNMIX_POSITION_TOP_REAR_CENTER][PA_DOWNMIX_POSITION_TOP_REAR_RIGHT] = 7071,

    /* downmix table for 7POINT1POINT4*/
    [PA_CHANNEL_LAYOUT_7POINT1POINT4][PA_CHANNEL_POSITION_FRONT_LEFT][PA_CHANNEL_POSITION_FRONT_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1POINT4][PA_CHANNEL_POSITION_FRONT_RIGHT][PA_CHANNEL_POSITION_FRONT_RIGHT] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1POINT4][PA_CHANNEL_POSITION_FRONT_CENTER][PA_CHANNEL_POSITION_FRONT_CENTER] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1POINT4][PA_CHANNEL_POSITION_LFE][PA_CHANNEL_POSITION_LFE] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1POINT4][PA_CHANNEL_POSITION_REAR_LEFT][PA_CHANNEL_POSITION_REAR_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1POINT4][PA_CHANNEL_POSITION_REAR_RIGHT][PA_CHANNEL_POSITION_REAR_RIGHT] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1POINT4][PA_CHANNEL_POSITION_SIDE_RIGHT][PA_CHANNEL_POSITION_SIDE_RIGHT] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1POINT4][PA_CHANNEL_POSITION_SIDE_LEFT][PA_CHANNEL_POSITION_SIDE_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1POINT4][PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER][PA_CHANNEL_POSITION_FRONT_LEFT] = 5946,
    [PA_CHANNEL_LAYOUT_7POINT1POINT4][PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER][PA_CHANNEL_POSITION_FRONT_CENTER] =
        7071,
    [PA_CHANNEL_LAYOUT_7POINT1POINT4][PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER][PA_CHANNEL_POSITION_FRONT_RIGHT] =
        5946,
    [PA_CHANNEL_LAYOUT_7POINT1POINT4][PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER][PA_CHANNEL_POSITION_FRONT_CENTER] =
        7071,
    [PA_CHANNEL_LAYOUT_7POINT1POINT4][PA_CHANNEL_POSITION_REAR_CENTER][PA_CHANNEL_POSITION_REAR_LEFT] = 7071,
    [PA_CHANNEL_LAYOUT_7POINT1POINT4][PA_CHANNEL_POSITION_REAR_CENTER][PA_CHANNEL_POSITION_REAR_RIGHT] = 7071,
    [PA_CHANNEL_LAYOUT_7POINT1POINT4][PA_DOWNMIX_POSITION_TOP_CENTER][PA_DOWNMIX_POSITION_TOP_FRONT_LEFT] = 5000,
    [PA_CHANNEL_LAYOUT_7POINT1POINT4][PA_DOWNMIX_POSITION_TOP_CENTER][PA_DOWNMIX_POSITION_TOP_FRONT_RIGHT] = 5000,
    [PA_CHANNEL_LAYOUT_7POINT1POINT4][PA_DOWNMIX_POSITION_TOP_CENTER][PA_DOWNMIX_POSITION_TOP_REAR_LEFT] = 5000,
    [PA_CHANNEL_LAYOUT_7POINT1POINT4][PA_DOWNMIX_POSITION_TOP_CENTER][PA_DOWNMIX_POSITION_TOP_REAR_RIGHT] = 5000,
    [PA_CHANNEL_LAYOUT_7POINT1POINT4][PA_DOWNMIX_POSITION_TOP_FRONT_LEFT][PA_DOWNMIX_POSITION_TOP_FRONT_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1POINT4][PA_DOWNMIX_POSITION_TOP_FRONT_RIGHT][PA_DOWNMIX_POSITION_TOP_FRONT_RIGHT] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1POINT4][PA_DOWNMIX_POSITION_TOP_FRONT_CENTER][PA_DOWNMIX_POSITION_TOP_FRONT_LEFT] = 7071,
    [PA_CHANNEL_LAYOUT_7POINT1POINT4][PA_DOWNMIX_POSITION_TOP_FRONT_CENTER][PA_DOWNMIX_POSITION_TOP_FRONT_RIGHT] = 7071,
    [PA_CHANNEL_LAYOUT_7POINT1POINT4][PA_DOWNMIX_POSITION_TOP_REAR_LEFT][PA_DOWNMIX_POSITION_TOP_REAR_LEFT] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1POINT4][PA_DOWNMIX_POSITION_TOP_REAR_RIGHT][PA_DOWNMIX_POSITION_TOP_REAR_RIGHT] = 10000,
    [PA_CHANNEL_LAYOUT_7POINT1POINT4][PA_DOWNMIX_POSITION_TOP_REAR_CENTER][PA_DOWNMIX_POSITION_TOP_REAR_LEFT] = 7071,
    [PA_CHANNEL_LAYOUT_7POINT1POINT4][PA_DOWNMIX_POSITION_TOP_REAR_CENTER][PA_DOWNMIX_POSITION_TOP_REAR_RIGHT] = 7071,
};

pa_channel_layout_index_t pa_channel_map_to_index(const pa_channel_map *map)
{
    pa_bitset_t in_map[PA_BITSET_ELEMENTS(PA_CHANNEL_POSITION_MAX)];
    unsigned c;

    pa_assert(map);

    pa_return_val_if_fail(pa_channel_map_valid(map), PA_CHANNEL_LAYOUT_OTHER);

    memset(in_map, 0, sizeof(in_map));

    for (c = 0; c < map->channels; c++) { pa_bitset_set(in_map, map->map[c], true); }

    if (pa_bitset_equals(in_map, PA_CHANNEL_POSITION_MAX, PA_CHANNEL_POSITION_LEFT, PA_CHANNEL_POSITION_RIGHT, -1)) {
        return PA_CHANNEL_LAYOUT_STEREO;
    }

    if (pa_bitset_equals(in_map, PA_CHANNEL_POSITION_MAX, PA_CHANNEL_POSITION_FRONT_LEFT,
        PA_CHANNEL_POSITION_FRONT_RIGHT, PA_CHANNEL_POSITION_SIDE_LEFT, PA_CHANNEL_POSITION_SIDE_RIGHT,
        PA_CHANNEL_POSITION_FRONT_CENTER, PA_CHANNEL_POSITION_LFE, -1)) { return PA_CHANNEL_LAYOUT_5POINT1; }

    if (pa_bitset_equals(in_map, PA_CHANNEL_POSITION_MAX, PA_CHANNEL_POSITION_FRONT_LEFT,
        PA_CHANNEL_POSITION_FRONT_RIGHT, PA_CHANNEL_POSITION_SIDE_LEFT, PA_CHANNEL_POSITION_SIDE_RIGHT,
        PA_CHANNEL_POSITION_FRONT_CENTER, PA_CHANNEL_POSITION_LFE, PA_CHANNEL_POSITION_TOP_REAR_LEFT,
        PA_CHANNEL_POSITION_TOP_REAR_RIGHT, -1)) { return PA_CHANNEL_LAYOUT_5POINT1POINT2; }

    if (pa_bitset_equals(in_map, PA_CHANNEL_POSITION_MAX, PA_CHANNEL_POSITION_FRONT_LEFT,
        PA_CHANNEL_POSITION_FRONT_RIGHT, PA_CHANNEL_POSITION_SIDE_LEFT, PA_CHANNEL_POSITION_SIDE_RIGHT,
        PA_CHANNEL_POSITION_FRONT_CENTER, PA_CHANNEL_POSITION_LFE, PA_CHANNEL_POSITION_TOP_REAR_LEFT,
        PA_CHANNEL_POSITION_TOP_REAR_RIGHT, PA_CHANNEL_POSITION_TOP_FRONT_LEFT,
        PA_CHANNEL_POSITION_TOP_FRONT_RIGHT, -1)) { return PA_CHANNEL_LAYOUT_5POINT1POINT4; }

    if (pa_bitset_equals(in_map, PA_CHANNEL_POSITION_MAX, PA_CHANNEL_POSITION_FRONT_LEFT,
        PA_CHANNEL_POSITION_FRONT_RIGHT, PA_CHANNEL_POSITION_SIDE_LEFT, PA_CHANNEL_POSITION_SIDE_RIGHT,
        PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT, PA_CHANNEL_POSITION_FRONT_CENTER,
        PA_CHANNEL_POSITION_LFE, -1)) { return PA_CHANNEL_LAYOUT_7POINT1; }

    if (pa_bitset_equals(in_map, PA_CHANNEL_POSITION_MAX, PA_CHANNEL_POSITION_FRONT_LEFT,
        PA_CHANNEL_POSITION_FRONT_RIGHT, PA_CHANNEL_POSITION_SIDE_LEFT, PA_CHANNEL_POSITION_SIDE_RIGHT,
        PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT, PA_CHANNEL_POSITION_FRONT_CENTER,
        PA_CHANNEL_POSITION_LFE, PA_CHANNEL_POSITION_TOP_REAR_LEFT,  PA_CHANNEL_POSITION_TOP_REAR_RIGHT, -1)) {
        return PA_CHANNEL_LAYOUT_7POINT1POINT2;
    }
    if (pa_bitset_equals(in_map, PA_CHANNEL_POSITION_MAX, PA_CHANNEL_POSITION_FRONT_LEFT,
        PA_CHANNEL_POSITION_FRONT_RIGHT, PA_CHANNEL_POSITION_SIDE_LEFT, PA_CHANNEL_POSITION_SIDE_RIGHT,
        PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT, PA_CHANNEL_POSITION_FRONT_CENTER,
        PA_CHANNEL_POSITION_LFE, PA_CHANNEL_POSITION_TOP_REAR_LEFT,  PA_CHANNEL_POSITION_TOP_REAR_RIGHT,
        PA_CHANNEL_POSITION_TOP_FRONT_LEFT, PA_CHANNEL_POSITION_TOP_FRONT_RIGHT, -1)) {
        return PA_CHANNEL_LAYOUT_7POINT1POINT4;
    }
    return PA_CHANNEL_LAYOUT_OTHER;
}

int pa_to_downmix_position(const pa_channel_position_t channel_position)
{
    if (channel_position < PA_CHANNEL_POSITION_AUX0) {
        return channel_position;
    } else if (channel_position > PA_CHANNEL_POSITION_AUX31) { /* skip AUX0 - AUX31 */
        return channel_position - SKIPPED_CHANNEL_COUNT;
    } else {
    /* AUX0 - AUX31, which are not in use now */
    /* do nothing to map table */
        return 0;
    }
}